package cn.newgrand.netcall.consultant.ui;

import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.CheckBoxList;
import org.jivesoftware.spark.util.ModelUtil;

import cn.newgrand.smackx.consultant.ConsultantManager;
import cn.newgrand.netcall.consultant.SearchManager;
import cn.newgrand.netcall.consultant.SearchManager.SeachType;
import cn.newgrand.smackx.consultant.packet.SpecialistSearch.SpecialistSearchField;
import cn.newgrand.netcall.consultant.ConsultantPlugin;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Builds the UI for any DataForm (JEP-0004: Data Forms), and allow for creation
 * of an answer form to send back the the server.
 */
public class FormPanel extends JPanel {
    private final Map<String,JComponent> valueMap = new HashMap<String,JComponent>();
    private int row = 5;
    private SearchManager.SeachType searchType = SearchManager.SeachType.detail;
    private String field1;
    private String field2;
    private String field3;
    private JTextField text;
    private JTextField text2;
    private  JComboBox box;
    private  JComboBox box2;
    private  JComboBox box3;
    private  JComboBox box4;
    private  JComboBox box5;
    private  JComboBox box6;
    private CardLayout layout;
    /**
     * Creates a new DataFormUI
     *
     * @param form the <code>DataForm</code> to build a UI with.
     */
    public FormPanel() {
        this.setLayout(new GridBagLayout());

        buildUI3();

        this.add(new JLabel(), new GridBagConstraints(0, row, 3, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    }

    public SearchManager.SeachType getSearchType(){
    	return searchType;
    }

    private void buildUI() {
    	
    	//Map<String,SpecialistSearchField>  defaultLabels = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSearchManager().getDefaultLabel();
    	final SearchManager searchManager = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSearchManager();
    	ButtonGroup group = new ButtonGroup();
    	JRadioButton radio1  = new JRadioButton("精确搜索");
    	JRadioButton radio2  = new JRadioButton("产品");
    	JRadioButton radio3  = new JRadioButton("领域");
    	JRadioButton radio4  = new JRadioButton("行业");
    	group.add(radio1);
    	group.add(radio2);
    	group.add(radio3);
    	group.add(radio4);
    	
    	ActionListener action = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String temp = ((JRadioButton)e.getSource()).getText();
				if(temp.equals("精确搜索")){
					text.setEnabled(true);
					text2.setEnabled(true);
					box.setEnabled(false);
					box2.setEnabled(false);
					box3.setEnabled(false);
					box4.setEnabled(false);
					box5.setEnabled(false);
					box6.setEnabled(false);
					searchType = SearchManager.SeachType.detail;
				}
				else if(temp.equals("产品")){
					text.setEnabled(false);
					text2.setEnabled(false);
					box.setEnabled(true);
					box2.setEnabled(true);
					box3.setEnabled(true);
					box4.setEnabled(false);
					box5.setEnabled(false);
					box6.setEnabled(false);
					searchType = SearchManager.SeachType.product;
				}
				else if(temp.equals("领域")){
					text.setEnabled(false);
					text2.setEnabled(false);
					box.setEnabled(false);
					box2.setEnabled(false);
					box3.setEnabled(false);
					box4.setEnabled(true);
					box5.setEnabled(true);
					box6.setEnabled(false);
					searchType  = SearchManager.SeachType.field;
				}
				else if(temp.equals("行业")){
					text.setEnabled(false);
					text2.setEnabled(false);
					box.setEnabled(false);
					box2.setEnabled(false);
					box3.setEnabled(false);
					box4.setEnabled(false);
					box5.setEnabled(false);
					box6.setEnabled(true);
					searchType = SearchManager.SeachType.trade;
				}
			}
    	};
    	
    	radio1.addActionListener(action);
    	radio2.addActionListener(action);
    	radio3.addActionListener(action);
    	radio4.addActionListener(action);
    	
        this.add(radio1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio2, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio3, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio4, new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(new JLabel("专家ID:"), new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text = new JTextField();
        this.add(text, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(new JLabel("专家用户:"), new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text2 = new JTextField();
        this.add(text2, new GridBagConstraints(4, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        this.add(new JLabel("产品:"), new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box = new JComboBox();
        box.setEditable(false);
//        SpecialistSearchField field = defaultLabels.get("产品");
//        if(field!=null){
//            List<String> values = field.getDefaultValues("产品");
//            if(values!=null){
//	            for(String value:values){
//	            	box.addItem(value);
//	            }
//            }
//        }
        
        box.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box.removeAllItems();
				
				List<String> strs = searchManager.getItems("产品");
				for(String str:strs){
					box.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        this.add(box, new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        this.add(new JLabel("套件:"), new GridBagConstraints(3, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box3 = new JComboBox();
        box3.setEditable(false);
//        SpecialistSearchField field3 = defaultLabels.get("产品");
//        if(field3!=null){
//            List<String> values3 = field3.getDefaultValues("套件");
//            if(values3!=null){
//	            for(String value3:values3){
//	            	box3.addItem(value3);
//	            }
//            }
//        }

        box3.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box3.removeAllItems();
					
				Object strtemp = box.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+str);
						for(String a:strs){
							box3.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        this.add(box3, new GridBagConstraints(4, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        
        this.add(new JLabel("模块:"), new GridBagConstraints(5, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box2 = new JComboBox();
        box2.setEditable(false);
//        SpecialistSearchField field2 = defaultLabels.get("产品");
//        if(field2!=null){
//            List<String> values2 = field2.getDefaultValues("模块");
//            if(values2!=null){
//	            for(String value2:values2){
//	            	box2.addItem(value2);
//	            }
//            }
//        }
        
        box2.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box2.removeAllItems();
				
				Object strtemp1 = box.getSelectedItem();
				Object strtemp2 = box3.getSelectedItem();
				
				if(strtemp1!=null){
					String str = (String)strtemp1;
					if(str.isEmpty()){
						return;
					}
				}
				
				if(strtemp2!=null){
					String str = (String)strtemp2;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+(String)strtemp1+"::"+str);
						for(String a:strs){
							box2.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        this.add(box2, new GridBagConstraints(6, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        this.add(new JLabel("领域:"), new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box4 = new JComboBox();
        box4.setEditable(false);
//        SpecialistSearchField field4 = defaultLabels.get("领域");
//        if(field4!=null){
//            List<String> values4 = field4.getDefaultValues("领域");
//            if(values4!=null){
//	            for(String value4:values4){
//	            	box4.addItem(value4);
//	            }
//            }
//        }
        
        box4.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box4.removeAllItems();
				
				List<String> strs = searchManager.getItems("领域");
				for(String str:strs){
					box4.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        this.add(box4, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        this.add(new JLabel("产品:"), new GridBagConstraints(3, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box5 = new JComboBox();
        box5.setEditable(false);
//        SpecialistSearchField field5 = defaultLabels.get("领域");
//        if(field5!=null){
//            List<String> values5 = field5.getDefaultValues("产品");
//            if(values5!=null){
//	            for(String value5:values5){
//	            	box5.addItem(value5);
//	            }
//            }
//        }

        box5.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
				box5.removeAllItems();
				
				Object strtemp = box4.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("领域::"+str);
						for(String a:strs){
							box5.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        this.add(box5, new GridBagConstraints(4, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        this.add(new JLabel("行业:"), new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        box6 = new JComboBox();
        box6.setEditable(false);
//        SpecialistSearchField field6 = defaultLabels.get("行业");
//        if(field6!=null){
//            List<String> values6 = field6.getDefaultValues("行业");
//            if(values6!=null){
//	            for(String value6:values6){
//	            	box6.addItem(value6);
//	            }
//            }
//        }
        
        box6.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
				box6.removeAllItems();
				
				List<String> strs = searchManager.getItems("行业");
				for(String str:strs){
					box6.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        this.add(box6, new GridBagConstraints(2, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
    }

    private void buildUI2(){
        final JPanel panelBottom  = new JPanel();
        JPanel panelTop = new JPanel();
        layout = new CardLayout();
    	final SearchManager searchManager = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSearchManager();
    	ButtonGroup group = new ButtonGroup();
    	JRadioButton radio1  = new JRadioButton("精确搜索");
    	JRadioButton radio2  = new JRadioButton("产品");
    	JRadioButton radio3  = new JRadioButton("领域");
    	JRadioButton radio4  = new JRadioButton("行业");
    	group.add(radio1);
    	group.add(radio2);
    	group.add(radio3);
    	group.add(radio4);
//    	panelTop.add(radio1);
//    	panelTop.add(radio2);
//    	panelTop.add(radio3);
//    	panelTop.add(radio4);
    	
    	ActionListener action = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String temp = ((JRadioButton)e.getSource()).getText();
				layout.show(panelBottom, temp);
				if(temp.equals("精确搜索")){
					searchType = SearchManager.SeachType.detail;
				}
				else if(temp.equals("产品")){
					searchType = SearchManager.SeachType.product;
				}
				else if(temp.equals("领域")){
					searchType  = SearchManager.SeachType.field;
				}
				else if(temp.equals("行业")){
					searchType = SearchManager.SeachType.trade;
				}
			}
    	};
    	
    	radio1.addActionListener(action);
    	radio2.addActionListener(action);
    	radio3.addActionListener(action);
    	radio4.addActionListener(action);
    	
        this.add(radio1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio2, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio3, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio4, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
       
        panelBottom.setLayout(layout);
        JPanel panel1  = new JPanel();
        panel1.setLayout(new GridBagLayout());
        JPanel panel2  = new JPanel();
        panel2.setLayout(new GridBagLayout());
        JPanel panel3  = new JPanel();
        panel3.setLayout(new GridBagLayout());
        JPanel panel4  = new JPanel();
        panel4.setLayout(new GridBagLayout());
        
        panelBottom.add(panel1, "精确搜索");
        panelBottom.add(panel2, "产品");
        panelBottom.add(panel3, "领域");
        panelBottom.add(panel4, "行业");
        
        //this.add(panelTop, new GridBagConstraints(0, 0, 4, 2, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        this.add(panelBottom, new GridBagConstraints(0, 1, 4, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(20, 5, 20, 5), 0, 0));
        
        panel1.add(new JLabel("专家ID:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text = new JTextField();
        panel1.add(text,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        panel1.add(new JLabel("专家用户:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text2 = new JTextField();
        panel1.add(text2,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel2.add(new JLabel("产品:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box = new JComboBox();
        box.setEditable(false);
        
        box.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box.removeAllItems();

				List<String> strs = searchManager.getItems("产品");
				for(String str:strs){
					box.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        panel2.add(box,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel2.add(new JLabel("套件:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box3 = new JComboBox();
        box3.setEditable(false);

        box3.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box3.removeAllItems();
				
				Object strtemp = box.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+str);
						for(String a:strs){
							box3.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        panel2.add(box3,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        
        panel2.add(new JLabel("模块:"),new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box2 = new JComboBox();
        box2.setEditable(false);

        box2.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box2.removeAllItems();
				
				Object strtemp1 = box.getSelectedItem();
				Object strtemp2 = box3.getSelectedItem();
				
				if(strtemp1!=null){
					String str = (String)strtemp1;
					if(str.isEmpty()){
						return;
					}
				}
				
				if(strtemp2!=null){
					String str = (String)strtemp2;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+(String)strtemp1+"::"+str);
						for(String a:strs){
							box2.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        panel2.add(box2,new GridBagConstraints(5, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel3.add(new JLabel("领域:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box4 = new JComboBox();
        box4.setEditable(false);
        box4.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				box4.removeAllItems();
				
				List<String> strs = searchManager.getItems("领域");
				for(String str:strs){
					box4.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        panel3.add(box4,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel3.add(new JLabel("产品:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box5 = new JComboBox();
        box5.setEditable(false);

        box5.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
				box5.removeAllItems();
				
				Object strtemp = box4.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("领域::"+str);
						for(String a:strs){
							box5.addItem(a);
						}
					}
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        
        panel3.add(box5,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel4.add(new JLabel("行业:"),new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        box6 = new JComboBox();
        box6.setEditable(false);
        
        box6.addPopupMenuListener(new PopupMenuListener() {
			
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
				box6.removeAllItems();
				
				List<String> strs = searchManager.getItems("行业");
				for(String str:strs){
					box6.addItem(str);
				}
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        panel4.add(box6,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
    }
    
    private void buildUI3(){
        final JPanel panelBottom  = new JPanel();
        JPanel panelTop = new JPanel();
        layout = new CardLayout();
    	final SearchManager searchManager = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSearchManager();
    	ButtonGroup group = new ButtonGroup();
    	JRadioButton radio1  = new JRadioButton("精确搜索");
    	JRadioButton radio2  = new JRadioButton("产品");
    	JRadioButton radio3  = new JRadioButton("领域");
    	JRadioButton radio4  = new JRadioButton("行业");
    	group.add(radio1);
    	group.add(radio2);
    	group.add(radio3);
    	group.add(radio4);
    	group.setSelected(radio1.getModel(),true);
//    	panelTop.add(radio1);
//    	panelTop.add(radio2);
//    	panelTop.add(radio3);
//    	panelTop.add(radio4);
    	
    	ActionListener action = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String temp = ((JRadioButton)e.getSource()).getText();
				layout.show(panelBottom, temp);
				if(temp.equals("精确搜索")){
					searchType = SearchManager.SeachType.detail;
				}
				else if(temp.equals("产品")){
					searchType = SearchManager.SeachType.product;
				}
				else if(temp.equals("领域")){
					searchType  = SearchManager.SeachType.field;
				}
				else if(temp.equals("行业")){
					searchType = SearchManager.SeachType.trade;
				}
			}
    	};
    	
    	radio1.addActionListener(action);
    	radio2.addActionListener(action);
    	radio3.addActionListener(action);
    	radio4.addActionListener(action);
    	
        this.add(radio1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio2, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio3, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        this.add(radio4, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
       
        panelBottom.setLayout(layout);
        JPanel panel1  = new JPanel();
        panel1.setLayout(new GridBagLayout());
        JPanel panel2  = new JPanel();
        panel2.setLayout(new GridBagLayout());
        JPanel panel3  = new JPanel();
        panel3.setLayout(new GridBagLayout());
        JPanel panel4  = new JPanel();
        panel4.setLayout(new GridBagLayout());
        
        panelBottom.add(panel1, "精确搜索");
        panelBottom.add(panel2, "产品");
        panelBottom.add(panel3, "领域");
        panelBottom.add(panel4, "行业");
        
        //this.add(panelTop, new GridBagConstraints(0, 0, 4, 2, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        this.add(panelBottom, new GridBagConstraints(0, 1, 4, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(20, 5, 20, 5), 0, 0));
        
        panel1.add(new JLabel("专家ID:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text = new JTextField();
        panel1.add(text,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        panel1.add(new JLabel("专家用户名:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        text2 = new JTextField();
        panel1.add(text2,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel2.add(new JLabel("产品:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box = new JComboBox();
        box.setEditable(false);
        
		List<String> strs = searchManager.getItems("产品");
		box.addItem("");
		for(String str:strs){
			box.addItem(str);
		}
		
		box.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				box3.removeAllItems();
				
				Object strtemp = box.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+str);
						box3.addItem("");
						for(String a:strs){
							box3.addItem(a);
						}
					}
				}
			}
		});
        
        panel2.add(box,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel2.add(new JLabel("套件:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box3 = new JComboBox();
        box3.setEditable(false);
        
        box3.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				box2.removeAllItems();
				
				Object strtemp1 = box.getSelectedItem();
				Object strtemp2 = box3.getSelectedItem();
				
				if(strtemp1!=null){
					String str = (String)strtemp1;
					if(str.isEmpty()){
						return;
					}
				}
				
				if(strtemp2!=null){
					String str = (String)strtemp2;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("产品::"+(String)strtemp1+"::"+str);
						box2.addItem("");
						for(String a:strs){
							box2.addItem(a);
						}
					}
				}
			}
		});
        
        panel2.add(box3,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        
        panel2.add(new JLabel("模块:"),new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box2 = new JComboBox();
        box2.setEditable(false);

        panel2.add(box2,new GridBagConstraints(5, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel3.add(new JLabel("领域:"),new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box4 = new JComboBox();
        box4.setEditable(false);
        box4.addItem("");
		List<String> strs2 = searchManager.getItems("领域");
		for(String str:strs2){
			box4.addItem(str);
		}
		
		box4.addItemListener(new  ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				box5.removeAllItems();
				
				Object strtemp = box4.getSelectedItem();
				if(strtemp!=null){
					String str = (String)strtemp;
					if(!str.isEmpty()){
						List<String> strs = searchManager.getItems("领域::"+str);
						box5.addItem("");
						for(String a:strs){
							box5.addItem(a);
						}
					}
				}
			}
		});

        panel3.add(box4,new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel3.add(new JLabel("产品:"),new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        box5 = new JComboBox();
        box5.setEditable(false);
        
        panel3.add(box5,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
        panel4.add(new JLabel("行业:"),new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        box6 = new JComboBox();
        box6.setEditable(false);
        box6.addItem("");
		List<String> strs3 = searchManager.getItems("行业");
		for(String str:strs3){
			box6.addItem(str);
		}

        panel4.add(box6,new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        
    }
    
    
    public String getField1(){
    	switch (this.searchType){
    	case detail:
    		this.field1 = this.text.getText();
    		break;
    	case product:
    		this.field1 =  (this.box.getSelectedItem()==null)?null:(String)this.box.getSelectedItem();
    		break;
    	case field:
    		this.field1 = (this.box4.getSelectedItem()==null)?null:(String)this.box4.getSelectedItem();
    		break;
    	case trade:
    		this.field1 = (this.box6.getSelectedItem()==null)?null:(String)this.box6.getSelectedItem();
    		break;
    	case none:
    		this.field1 = null;
    		break;
    	}
		if(field1!=null&&field1.equals(""))
			field1=null;
    	return this.field1;
    }
    
    public String getField2(){
    	switch (this.searchType){
    	case detail:
    		this.field2 = this.text2.getText();
    		break;
    	case product:
    		this.field2 = (box2.getSelectedItem()==null)?null:(String)box2.getSelectedItem();
    		break;
    	case field:
    		this.field2 = (box5.getSelectedItem()==null)?null:(String)box5.getSelectedItem();
    		break;
    	case trade:
    		this.field2 = null;
    		break;
    	case none:
    		this.field2 = null;
    		break;
    	}
		if(field2!=null&&field2.equals(""))
			field2=null;
    	return this.field2;
    }
    
    public String getField3(){
    	switch (this.searchType){
    	case detail:
    		this.field3 = null;
    		break;
    	case product:
    		this.field3 = (box3.getSelectedItem()==null)?null:(String)box3.getSelectedItem();
    		break;
    	case field:
    		this.field3 = null;
    		break;
    	case trade:
    		this.field3 = null;
    		break;
    	case none:
    		this.field3 = null;
    		break;
    	}
		if(field3!=null&&field3.equals(""))
			field3=null;
    	return this.field3;
    }
    
    private void addField(String label, JComponent comp, String variable) {
        if (!(comp instanceof JCheckBox)) {
            this.add(new JLabel(label), new GridBagConstraints(0, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        }
        if (comp instanceof JTextArea) {
            this.add(new JScrollPane(comp), new GridBagConstraints(1, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 100, 50));
        }
        else if (comp instanceof JCheckBox) {
            this.add(comp, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        }
        else if (comp instanceof CheckBoxList) {
            this.add(comp, new GridBagConstraints(1, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 50));
        }
        else {
            this.add(comp, new GridBagConstraints(1, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

        }
        valueMap.put(variable, comp);
        row++;
    }

    public Component getComponent(String label) {
        return valueMap.get(label);
    }
}

